package com.group.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbBillLogMapper;
import com.group.dao.TbBillMapper;
import com.group.dao.TbVerifyMapper;
import com.group.pojo.Common;
import com.group.pojo.TbBill;
import com.group.pojo.TbBillLog;
import com.group.pojo.TbUser;
import com.group.pojo.TbVerify;
import com.group.pojo.TbVerifyExample;
import com.group.pojo.TbVerifyExample.Criteria;
import com.group.service.CommonService;
import com.group.service.VerifyService;
import com.group.util.CommonUtil;

/*
 * 0---拒绝
 * 1---通过
 */
@Service
public class VerifyServiceImpl implements VerifyService {
	@Autowired
	private TbVerifyMapper verifyMapper;
	@Autowired
	private CommonService commonService;
	@Autowired
	private TbBillMapper billMapper;
	@Autowired
	private TbBillLogMapper logMapper;
	

	private void setCondition(Criteria criteria, TbVerify verify) {
		String id = verify.getId();
		if (id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		String verifyResult = verify.getVerifyResult();
		if (verifyResult != null && !"".equals(verifyResult)) {
			criteria.andVerifyResultEqualTo(verifyResult);
		}
		
		String verifyUserId = verify.getVerifyUserId();
		if (verifyUserId != null && !"".equals(verifyUserId)) {
			criteria.andVerifyUserIdEqualTo(verifyUserId);
		}
	}
	
	/**
	 * 审核提交
	 * 1) 记录插入到审核表
	 * 2) 更新bill表中的状态，0:审核拒绝，1待审核，2审核通过，待提交维修地址，3维修地址已提交，待维修师抢单，4维修师已抢单，待维修，5维修师已完成，待评价 6维修已完成
	 * 3) 新插入一张记录在billlog表中
	 */
	@Override
	public Map<String, String> save(TbVerify verify, HttpServletRequest request) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		
		String nextId = commonService.getTbCurrentDateNextId(new Common("tb_verify", CommonUtil.getCurrentDate("yyyyMMdd")));
		verify.setId(nextId);
		
		TbUser loginUser = (TbUser) request.getSession().getAttribute("USER_SESSION");
		verify.setVerifyUserId(loginUser.getId());
		verify.setVerifyTime(new Date());
		
		int insert = verifyMapper.insert(verify);
		if (insert == 1) {
			TbBill bill = new TbBill();
			bill.setId(verify.getBillId());
			if ("1".equals(verify.getVerifyResult())) {
				//审核通过
				bill.setBillStatus("2");
			} else {
				bill.setBillStatus("0");
			}
			int update = billMapper.updateByPrimaryKeySelective(bill);
			if (update == 1) {
				TbBillLog log = new TbBillLog();
				String logNextId = commonService.getTbCurrentDateNextId(new Common("tb_bill_log", CommonUtil.getCurrentDate("yyyyMMdd")));
				log.setId(logNextId);
				log.setBillId(verify.getBillId());
				if ("1".equals(verify.getVerifyResult())) {
					//审核通过
					log.setOperateEvent("审核通过");
				} else {
					log.setOperateEvent("审核拒绝，原因："+verify.getVerifyReason());
				}
				log.setOperateUserId(loginUser.getId());
				log.setOperateUserType("2");//工作人员
				log.setOperateTime(new Date());
				int insert2 = logMapper.insert(log);
				if (insert2 == 1) {
					returnMap.put("success", "true");
				}
			}
		}
		return returnMap;
	}

	@Override
	public List<TbVerify> query(TbVerify verify) {
		TbVerifyExample example = new TbVerifyExample();
		
		setCondition(example.createCriteria(), verify);
		
		List<TbVerify> list = verifyMapper.selectByExample(example);
		
		return list;
	}


	 

}
